#include <iostream>
#include <curl/curl.h>

// 回调函数处理响应数据
size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* output) {
    size_t total_size = size * nmemb;
    output->append((char*)contents, total_size);
    return total_size;
}

int main() {
    CURL* curl;
    CURLcode res;
    std::string response;

    curl = curl_easy_init();
    if(curl) {
        // 设置请求URL
        curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/api");
        
        // 设置POST数据
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=John&age=30");
        
        // 设置回调函数
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
        
        // 执行请求
        res = curl_easy_perform(curl);
        
        if(res != CURLE_OK) {
            std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
        } else {
            std::cout << "Response: " << response << std::endl;
        }
        
        curl_easy_cleanup(curl);
    }
    return 0;
}